package com.example.dllo.keepdemo.dynamic.attention;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.dynamic.bean.AttentionBean;
import com.example.dllo.keepdemo.greendao.DynamicAttention;
import com.example.dllo.keepdemo.tools.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private AttentionBean data;
    private List<DynamicAttention> dynamicAttentionList;
    //test = 1代表无网
    private int test;

    public ListViewAdapter(Context context,AttentionBean data,int hasNet) {
        this.context = context;
        this.data = data;
        this.test = hasNet;
    }

    public ListViewAdapter(List<DynamicAttention> dynamicAttentionList, Context context,int noneNet) {
        this.dynamicAttentionList = dynamicAttentionList;
        this.context = context;
        this.test = noneNet;
    }

    @Override
    public int getCount() {
        if (test == 1) {//没网
            return dynamicAttentionList.size();
        } else {
            return data.getData().size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_fragment_attention,parent,false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else  {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        if (test == 1) {
            myViewHolder.tvTitle.setText(dynamicAttentionList.get(position).getTitle());

            ImageView[] imageViews = {myViewHolder.img1,myViewHolder.img2,myViewHolder.img3};
            String[] url = {dynamicAttentionList.get(position).getImage1(),dynamicAttentionList.get(position).getImage2(),dynamicAttentionList.get(position).getImage3()};
            for (int i = 0; i < 3; i++) {
//                ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageViews[i],R.mipmap.placeholder41_41,R.mipmap.no_photo);
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageViews[i],R.mipmap.placeholder41_41,R.mipmap.no_photo);
                VolleySingleton.getImageLoader().get(url[i],listener,120,110);
            }
        } else {
            myViewHolder.tvTitle.setText(data.getData().get(position).getUser().getUsername());

            ImageView[] imageViews = {myViewHolder.img1,myViewHolder.img2,myViewHolder.img3};
            for (int i = 0; i < 3; i++) {
                ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageViews[i],R.mipmap.placeholder41_41,R.mipmap.no_photo);
                VolleySingleton.getImageLoader().get(data.getData().get(position).getEntries().get(i).getPhoto(),listener,120,110);
            }
        }



        return convertView;
    }

    class MyViewHolder {
        private TextView tvTitle;
        private ImageView img1,img2,img3;
        public MyViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_item_fragment_attention_title);
            img1 = (ImageView) view.findViewById(R.id.img_item_fragment_attention_img1);
            img2 = (ImageView) view.findViewById(R.id.img_item_fragment_attention_img2);
            img3 = (ImageView) view.findViewById(R.id.img_item_fragment_attention_img3);
        }
    }
}
